package GraphLayer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;

public class Graph extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Color> colors;

	private Graphics g;
	
	private final int extendX = 100;
	private final int extendY = 0;
	
	private final int XZERO = 10+extendX;
	private final int YZERO = 10+extendY;
	private final int YMAX = 300+extendY;
	private final int XMAX = 590+extendX;

    @Override
    public void paintComponent(Graphics g) {
    	this.g = g;
        super.paintComponent(this.g);
        colors = new ArrayList<Color>();
        
        {
	        colors.add(new Color(115, 144, 241));
	        colors.add(new Color(115, 197, 241));
	        colors.add(new Color(241, 115, 136));
	        colors.add(new Color(227, 50, 50));
	        colors.add(new Color(88, 50, 227));
	        colors.add(new Color(50, 227, 53));
	        colors.add(new Color(118, 227, 50));
        }
        
        drawFrame();
        drawData();
    }
    
    private void drawFrame(){
    	Graphics2D g2d = (Graphics2D) g;
    	g2d.setColor(new Color(222, 131, 27));
    	g2d.setStroke(new BasicStroke(2));
        g2d.drawLine(XZERO, YZERO, XZERO, YMAX);
        g2d.drawLine(XZERO, YMAX, XMAX, YMAX);
        for(int i = YMAX-10; i > YZERO; i -= 10){
        	g2d.drawLine(XZERO, i, XZERO-5, i);
        }
        for(int i = XMAX-20; i > XZERO; i -= 20){
        	g2d.drawLine(i, YMAX, i, YMAX+5);
        }
    }
    
    private void drawNameBox(String name, Graphics2D g2d, int counter){
    	int y = 10;
    	int yInc = 15;
    	
    	if(counter == 0){
    		y = 10;
    	}else{
    		y = 10 + (counter * 20); 
    	}
    	
    	g2d.drawLine(5, y, extendX-5, y);
    	g2d.drawLine(extendX-5, y, extendX-5, y+yInc);
    	g2d.drawLine(extendX-5, y+yInc, 5, y+yInc);
    	g2d.drawLine(5, y+yInc, 5, y);
    	g2d.drawString(name, 5+2, y+yInc-3);
    }
    
    private void drawData(){
    	int counter = 0;
    	Graphics2D g2d = (Graphics2D) g;
    	GraphCtr gCtr = new GraphCtr();
    	ArrayList<Flavour> flavours = gCtr.drawThis();
    	if(flavours.size() > 0){
	    	for(Flavour f : flavours){    		
	    		g2d.setStroke(new BasicStroke(2));
	    		g2d.setColor(colors.get(counter));
		    	ArrayList<int[]> gList = gCtr.buildWeeks(f);
		    	
		    	int x = XMAX;
		    	int y = YMAX;
		    	
		    	drawNameBox(f.getFlavour(), g2d, counter);
		    	counter++;
		    	// i[0] - Amount
		    	// i[1] - Date
		    	for(int[] i : gList){
		    		int y1 = (YMAX-(i[0]*10));
		    		int x1 = (XMAX-(i[1]*20));
		    		if(XZERO > x1){
		    			x1 = XZERO;
		    			y1 = y;
		    		}
		    		g2d.drawLine(x, y, x1, y1);
		    		if(y1 < y){
		    			g2d.drawString(Integer.toString(i[0]), x1-2, y1-2);
		    		}else if(y1 > y){
		    			g2d.drawString(Integer.toString(i[0]), (x1+2), (y1+10));
		    		}
		    		y = y1;
		    		x = x1;
		    	}
	    	}
    	}
    }
}